package com.qiezi.project.fragment;



import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;
import com.flyco.tablayout.SlidingTabLayout;
import com.qiezi.R;
import com.qiezi.base.BaseMvpFragment;
import com.qiezi.bean.CategoryBean;
import com.qiezi.bean.DataBean;
import com.qiezi.bean.ResponseBean;
import com.qiezi.util.Request;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class CategoryFragment extends BaseMvpFragment {


    private class MyPagerAdapter extends FragmentPagerAdapter {
        private ArrayList<Fragment> mFragments = new ArrayList();
        private ArrayList<String> mTitles = new ArrayList<>();

        public MyPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        public void setCategory(List<CategoryBean> categorys){
            Collections.sort(categorys);
            for (CategoryBean b : categorys){
                mTitles.add(b.getName());
                CategoryOneFragment fragment = new CategoryOneFragment();
                fragment.setId(b.getId());
                mFragments.add(fragment);
            }
        }

        public int getCount() {
            return this.mFragments.size();
        }

        public Fragment getItem(int index) {
            return mFragments.get(index);
        }

        public CharSequence getPageTitle(int index) {
            return mTitles.get(index);
        }
    }

    @BindView(R.id.tabs)
    SlidingTabLayout slidingTabLayout;

    @BindView(R.id.pager)
    ViewPager vp;

    @BindView(R.id.rl_error)
    RelativeLayout rlError;

    @BindView(R.id.tv_error_hint)
    TextView rvErrorHint;

    @BindView(R.id.iv_error)
    ImageView ivError;

    public void showFirstPage(){
        slidingTabLayout.setCurrentTab(0,false);
        allScrollToTop();
    }

    public void allScrollToTop(){
        for (int i=0;i<adapter.getCount();i++){
            ((BaseMvpFragment)adapter.getItem(i)).scrollToTop();
        }
    }

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_home;
    }

    MyPagerAdapter adapter;

    @Override
    protected void initData(Bundle savedInstanceState) {
        adapter = new MyPagerAdapter(getChildFragmentManager());
//        empty();
        loadTabs();

    }

    private Map<String,String> params;

    private void loadTabs(){

        Request.post(Request.Api.getCategoryOne, null, new Request.JsonCallback<DataBean<CategoryBean>>(new TypeReference<DataBean<CategoryBean>>(){}.getType()) {



            @Override
            public void onFailure(String msg) {
                showError(msg);
            }

            @Override
            public void onSueccess(ResponseBean<DataBean<CategoryBean>> data) {
                setTabs(data.getData().getList());
            }


        });
    }


    @Override
    public void showError(String msg) {
        super.showError(msg);
        rvErrorHint.setText("请求失败，点击重试");
        if (rlError.getVisibility() != View.VISIBLE){
            rlError.setVisibility(View.VISIBLE);
        }
    }


    private void empty(){
        rlError.setVisibility(View.VISIBLE);
        ivError.setImageResource(R.drawable.ic_data_empty);
        rvErrorHint.setText("暂无分类");
        rlError.setEnabled(false);
    }

    private void setTabs(List<CategoryBean> list){
        if (list == null || list.size()==0)empty();
        if (rlError.getVisibility() == View.VISIBLE){
            rlError.setVisibility(View.GONE);
        }
        adapter.setCategory(list);
        vp.setAdapter(adapter);
        slidingTabLayout.setViewPager(vp);
    }

    @Override
    protected void initListener() {
        rlError.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!rvErrorHint.getText().toString().equals("重试中...")){
                    loadTabs();
                    rvErrorHint.setText("重试中...");
                }
            }
        });
    }
}
